package com.example.weli.ble2test;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.style.RelativeSizeSpan;
import android.text.style.SubscriptSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class ColorLEDController {
    private UIActivity mActivity;
    private Drawable mBlue;
    private Drawable mGreen;
    private int mLEDNumber;
    private Drawable mRed;

    class LabelClickListener implements OnClickListener {
        private final Slider mSlider;
        private final double mValue;

        public LabelClickListener(Slider slider, double value) {
            this.mSlider = slider;
            this.mValue = value;
        }

        public void onClick(View v) {
        }
    }

    class LedValueUpdater implements Slider.SliderPositionListener {
        private final byte mCommandTarget;
        private TextView mTarget;

        LedValueUpdater(TextView target, int colorIndex) {
            this.mTarget = target;
            this.mCommandTarget = (byte) (((ColorLEDController.this.mLEDNumber - 1) * 3) + colorIndex);
        }

        public void onPositionChange(double value) {
            int v = (int) value;
            this.mTarget.setText(String.valueOf(v));
            if (ColorLEDController.this.mActivity != null) {
                ColorLEDController.this.mActivity.sendCommand((byte) 2, this.mCommandTarget, (byte) v);
            }
        }

        public void disPosition(double value) {
            this.mTarget.setText(String.valueOf((int) value));
        }
    }

    @SuppressLint("ResourceType")
    public ColorLEDController(UIActivity activity, int number, Resources res, boolean vertical) {
        this.mActivity = activity;
        this.mLEDNumber = number;
        if (vertical) {
            this.mRed = res.getDrawable(R.color.LightGray);
            this.mGreen = res.getDrawable(R.color.grey33);
            this.mBlue = res.getDrawable(R.color.Baclk);
            return;
        }
        this.mRed = res.getDrawable(R.color.LightGray);
        this.mGreen = res.getDrawable(R.color.grey33);
        this.mBlue = res.getDrawable(R.color.Baclk);
    }

    public void attachToView(ViewGroup targetView) {
        for (int i = 0; i < 3; i++) {
            ViewGroup g = (ViewGroup) targetView.getChildAt(i);
            TextView label = (TextView) g.getChildAt(0);
            Slider slider = (Slider) g.getChildAt(1);
            TextView valueText = (TextView) g.getChildAt(2);
            slider.setPositionListener(new LedValueUpdater(valueText, i));
            label.setOnClickListener(new LabelClickListener(slider, 0.0d));
            valueText.setOnClickListener(new LabelClickListener(slider, 1.0d));
            valueText.setText("0");
            if (i == 0) {
                String labelText = "Led";
                SpannableStringBuilder ssb = new SpannableStringBuilder(labelText);
                ssb.append(String.valueOf(this.mLEDNumber));
                int spanStart = labelText.length();
                int spanEnd = spanStart + 1;
                ssb.setSpan(new SubscriptSpan(), spanStart, spanEnd, 0);
                ssb.setSpan(new RelativeSizeSpan(0.7f), spanStart, spanEnd, 0);
                slider.setSliderBackground(this.mRed);
            } else if (i == 1) {
                slider.setSliderBackground(this.mGreen);
            } else {
                slider.setSliderBackground(this.mBlue);
            }
        }
    }
}
